package infastructure;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.util.StringUtils;
import opennlp.tools.sentdetect.*;

public class testReg {
	
	public static final String TEXTPATH="C:\\study\\technion\\MSc\\semester1\\Y!\\dumpTest\\";
	public static final  String PWDNLP= "C:\\softwares\\apache-opennlp-1.5.3\\";
	private static List<String> movieReceptionPattern =Arrays.asList(
			"==[R|r]eception==((===)[^=]|([^=]=[^=])|[^=])*==",
			"==Critical reception==((===)[^=]|([^=]=[^=])|[^=])*==",		
			"==[R|r]elease==((===)[^=]|([^=]=[^=])|[^=])*=="
	);

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException 
	{
		
		wikiReader wr = new wikiReader();
		PrintWriter writer = new PrintWriter(TEXTPATH +"bladeRunnerSentences.txt");
		//BufferedReader  br = new BufferedReader(new FileReader("C:\\study\\technion\\MSc\\semester1\\Y!\\dumpTest\\Army of Darkness.txt"));
		BufferedReader  br = new BufferedReader(new FileReader("C:\\study\\technion\\MSc\\semester1\\Y!\\dumpTest\\TheBigO.txt"));
		StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        String[] sentences=null;

        while (line != null) 
        {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        String everything = sb.toString();
        br.close(); 
//        //wr.ParseSentenceAndSaveFile(everything, "armyOfDarkness.txt", "armyOfDarkness", "1");
//
        // try using OpenNLP
//        try 
//        {
//        	InputStream stream = new ByteArrayInputStream(everything.getBytes("UTF-8"));
//        	SentenceModel sentenceModel = new SentenceModel(stream);
//            SentenceDetector SD = new SentenceDetectorME(sentenceModel);
//            sentences=SD.sentDetect(everything);
//            everything="";
//            StringBuilder sBuilder = new StringBuilder();
//            for (int i=0; i<sentences.length; i++)
//            {
//            	sBuilder.append("<s>" +(sentences[i]) + "</s>");
//            	sBuilder.append('\n');	  
//            }
//            everything = sBuilder.toString();
//	   	     writer.write(everything);
//	   	     writer.close();
//        }
//        catch (Exception e)
//        {
//        	System.out.println(e.getMessage());
//        } 
//	 //using regExp - did not work
//		  Pattern pattern=null;
//		  Matcher matcher=null;
//		  List<String> content = new ArrayList<String>();
//		  List<String> patternList = new ArrayList<>();
//		  String section="reception";
//		  switch (section)
//		  {
//		  case "reception":
//			  patternList= movieReceptionPattern;
//		  		break;
//		  }
		 try 
		 {
			 
			// String[] sections= everything.split("==[R|r]eception==((===)[^=]|([^=]=[^=])|[^=])*==");
			 String receptionText= everything.split("==Reception==")[0];
			 String receptionText2 =receptionText.split("[^=]==[^=]")[0];
			 String cleanText =wr.wikiTextToPlainText(receptionText2); 
			// PrintWriter pWriter = new PrintWriter(TEXTPATH +"reception.txt");
			// pWriter.write(cleanText);
			// pWriter.close();
			 
			 //parse SENTENCES
			 everything="";
			 InputStream modelIn = new FileInputStream(PWDNLP+"en-sent.bin");

			 try 
			 {
			   SentenceModel model = new SentenceModel(modelIn);
			   SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
			   sentences = sentenceDetector.sentDetect(cleanText);
			   StringBuilder sBuilder = new StringBuilder();
			   //PrintWriter sentWriter = new PrintWriter(TEXTPATH+"receptionSentences.txt");
	            for (int i=0; i<sentences.length; i++)
	            {
	            	sBuilder.append("<s>" +(sentences[i]) + "</s>");
	            	sBuilder.append('\n');	  
	            }
	            everything = sBuilder.toString();
	            writer.write(everything);
	            writer.close();
			   
			 }
			 catch (IOException e) 
			 {
			   e.printStackTrace();
			 }
			 finally 
			 {
			   if (modelIn != null) {
			     try {
			       modelIn.close();
			     }
			     catch (IOException e) {
			     }
			   }
			 }
			 
			 
			 
			 //wr.ParseSentenceAndSaveFile(wr.wikiTextToPlainText(receptionText),"receptionParsed.txt", "reception", "1");
			 //wr.ParseSentenceAndSaveFile(receptionText, "reception.txt", "armyOfDarkness", "1");
			//System.out.println(cleanText);
		 }
			   
		  //for (String regPattern : patternList)
		  //{
//			  String regPattern = "==[R|r]eception==((===)[^=]|([^=]=[^=])|[^=])*==";
//			  Pattern pattern = Pattern.compile(regPattern);
//			  matcher = pattern.matcher(everything);
//			 try 
//			 {
//			  while (matcher.find()) 
//			  {
//				   content.add(matcher.group());
//				   System.out.println(content.toString());
//			  }
//			 }
//			  }
		// }
			 catch (Exception e)
			 {
				 System.out.println(e.getMessage());
			 }
			//  if (content != null && content.size() > 0)
				 // break;
		  //} 
		 
		 
		// TODO Auto-generated method stub

	}

}
